<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="crud.queryInput.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="crud.queryInput.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="600px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="160px">
          <el-form-item label="项目">
            <!-- 手动设置 Select -->
            <project-select v-model="form.projectId" />
          </el-form-item>
          <el-form-item label="重要程度">
            <!-- 手动设置 Select -->
            <el-radio-group v-model="form.importance" style="width: 178px">
              <el-radio label="一般">一般</el-radio>
              <el-radio label="重要">重要</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="主要完成形式/成果">
            <el-input v-model="form.achievement" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="计划开始时间">
            <el-date-picker v-model="form.planStartTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="计划完成时间">
            <el-date-picker v-model="form.planFinishTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="实际开始时间">
            <el-date-picker v-model="form.startTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="实际完成时间">
            <el-date-picker v-model="form.finishTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="部门项目工作文档位置">
            <el-input v-model="form.docPath" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="计划依据">
            <el-input v-model="form.planBasis" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="项目密级">
            <!-- 手动设置 Select -->
            <el-radio-group v-model="form.secretLevel" style="width: 370px">
              <el-radio label="非秘">非秘</el-radio>
              <el-radio label="秘密">秘密</el-radio>
              <el-radio label="机密">机密</el-radio>
              <el-radio label="绝密">绝密</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="研制内容">
            <el-input v-model="form.contents" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="部门项目管理文档位置">
            <el-input v-model="form.mngDocPath" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('id')" prop="id" label="ID" />
        <el-table-column v-if="columns.visible('projectId')" prop="projectBase.projectName" label="项目" />
        <el-table-column v-if="columns.visible('importance')" prop="importance" label="重要程度" />
        <el-table-column v-if="columns.visible('achievement')" prop="achievement" label="主要完成形式/成果" />
        <el-table-column v-if="columns.visible('planStartTime')" prop="planStartTime" label="计划开始时间" width="100px">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.planStartTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('planFinishTime')" prop="planFinishTime" label="计划完成时间" width="100px">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.planFinishTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('startTime')" prop="startTime" label="实际开始时间" width="100px">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('finishTime')" prop="finishTime" label="实际完成时间" width="100px">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('docPath')" prop="docPath" label="部门项目工作文档位置" />
        <el-table-column v-if="columns.visible('planBasis')" prop="planBasis" label="计划依据" />
        <el-table-column v-if="columns.visible('secretLevel')" prop="secretLevel" label="项目密级" />
        <el-table-column v-if="columns.visible('contents')" prop="contents" label="研制内容" />
        <el-table-column v-if="columns.visible('mngDocPath')" prop="mngDocPath" label="部门项目管理文档位置" />
        <el-table-column v-permission="['admin','projectPlan:edit','projectPlan:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudProjectPlan from '@/api/projectPlan'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import projectSelect from '@/views/components/cascader/project_cascader'

// crud交由presenter持有
// defaultCrud默认不加载数据
const defaultCrud = CRUD({ queryOnPresenterCreated: false, title: '项目计划', url: 'api/projectPlan', sort: 'id,desc', crudMethod: { ...crudProjectPlan }})
const defaultForm = { id: null, projectId: null, importance: null, achievement: null, planStartTime: null, planFinishTime: null, startTime: null, finishTime: null, docPath: null, planBasis: null, secretLevel: null, contents: null, mngDocPath: null }
export default {
  name: 'ProjectPlan',
  components: { pagination, crudOperation, rrOperation, udOperation, projectSelect },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      permission: {
        add: ['admin', 'projectPlan:add'],
        edit: ['admin', 'projectPlan:edit'],
        del: ['admin', 'projectPlan:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'id', display_name: 'ID' },
        { key: 'projectId', display_name: '项目ID' },
        { key: 'projectName', display_name: '项目' },
        { key: 'importance', display_name: '重要程度' },
        { key: 'achievement', display_name: '主要完成形式/成果' },
        { key: 'docPath', display_name: '部门项目工作文档位置' },
        { key: 'planBasis', display_name: '计划依据' },
        { key: 'secretLevel', display_name: '项目密级' },
        { key: 'contents', display_name: '研制内容' },
        { key: 'mngDocPath', display_name: '部门项目管理文档位置' }
      ]
    }
  },
  created() {
    // 初始化搜索条件
    this.crud.query.type = null
    this.crud.queryInput.value = null
    this.crud.query = {}
    // 设置默认搜索条件
    if (this.$route.query.mode === 'ONE') {
      this.crud.queryInput.type = 'projectId'
      this.crud.queryInput.value = this.$route.query.projectId
    }
    // 注意这里需要手动refreash加载数据，如果crud自动加载数据，则无法保证设置query发生在refreah之前
    this.crud.refresh()
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      // 叠加queryInput中的搜索条件
      if (this.crud.queryInput.type && this.crud.queryInput.value) {
        this.crud.query[this.queryInput.type] = this.queryInput.value
      }
      return true
    },
    afterCrudRefresh() {
      // 无记录则显示新建dialog
      if (this.crud.data.length === 0) {
        this.form.projectId = this.$route.query.projectId
        this.crud.toAdd()
      }
    }
  }
}
</script>

<style scoped>

</style>
